package com.bjpowernode.web.controller;

import com.bjpowernode.pojo.User;
import com.bjpowernode.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("test") // 作为模块名，方便团队开发
public class TestController {

    @Autowired
    private ProductService productService;

    @Autowired
    private HttpServletRequest request;
    @Resource
    private HttpServletResponse response;

    // "/test/test01.html"
    @RequestMapping("test01.html")
    public String test01() {
        //System.out.println(productService);
        //System.out.println(1/0);
        //return "/WEB-INF/jsp/success.jsp";
        return "success"; // 配置视图前缀和后缀之后
    }

    // 该方法只能使用post请求方式来访问
    @RequestMapping(value = "test02.html", method = RequestMethod.POST)
    public String test02() {
        //return "/WEB-INF/jsp/success.jsp";
        return "success"; // 配置视图前缀和后缀之后
    }

    // 必须携带name参数和age参数，age参数的值必须是20
    @RequestMapping(value = "test03.html", params = {"name","age=20"})
    public String test03() {
        //return "/WEB-INF/jsp/success.jsp";
        return "success"; // 配置视图前缀和后缀之后
    }

    // 基本类型和String类型的参数，方法中的参数名和请求传递的参数名一致
    @RequestMapping("test04.html")
    public String test04(String name, int age) {
        System.out.println(name);
        System.out.println(age);
        return "success";
    }

    // 请求参数名要和类中的属性名一致
    @RequestMapping("test05.html")
    public String test05(User user) {
        System.out.println(user);
        return "success";
    }

    @RequestMapping("test06.html")
    public String test06(String[] strs) {
        for (String str : strs) {
            System.out.println(str);
        }
        return "success";
    }

    @RequestMapping("test07.html")
    public String test07(String username) {
        System.out.println(username);
        return "success";
    }

    @RequestMapping("test08.html")
    public String test08(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        System.out.println(date);
        return "success";
    }

    @RequestMapping("test09.html")
    public String test09(Date date) {
        System.out.println(date);
        return "success";
    }

    @RequestMapping("test10.html")
    public String tes10(@RequestParam(defaultValue = "hello", value = "username") String name,
                        @RequestHeader(value = "User-Agent", required = false) String ua,
                        @CookieValue(value = "JSESSIONID", required = false, defaultValue = "xxxx") String jsessionid) {
        System.out.println(name);
        System.out.println(ua);
        System.out.println(jsessionid);
        return "success";
    }

    @RequestMapping("test11.html")
    public void tes11(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String name2 = this.request.getParameter("name");
        System.out.println(name);
        System.out.println(name2);
        response.getWriter().write("hello");
        this.response.getWriter().write(" world!");
    }

    @RequestMapping("test12.html")
    public String tes12() {
        return "redirect:/static/css/1.css";
        // WEB-INF是一个特殊目录：该目录下的资源不能通过浏览器的地址栏直接访问！
        //return "redirect:/WEB-INF/jsp/success.jsp";
    }

    @RequestMapping("test13.html")
    public ModelAndView tes13(ModelAndView mv) {
        //ModelAndView mv = new ModelAndView();
        // 向request中存放数据
        mv.addObject("username", "zhangsan");
        mv.setViewName("success");
        return mv;
    }

    @RequestMapping("test14.json")
    @ResponseBody // 将方法的返回值转换为json，并使用response对象进行响应
    public User tes14() {
        User user = new User();
        user.setUsername("zhangsan");
        return user;
    }

    @RequestMapping("save.do")
    public void save(User user, HttpServletResponse response) throws IOException {
        System.out.println(user);
        response.getWriter().write("save success!");
    }

    @RequestMapping("save2.do")
    // @RequestBody: 将请求中json类型的数据转换为该注解标注的参数类型
    public void save2(@RequestBody User user, HttpServletResponse response) throws IOException {
        System.out.println(user);
        response.getWriter().write("save success!");
    }

    @RequestMapping("save3.do")
    // @RequestBody: 将请求中json类型的数据转换为该注解标注的参数类型
    public void save2(@RequestBody List<User> users, HttpServletResponse response) throws IOException {
        System.out.println(users);
        response.getWriter().write("save success!");
    }

    @RequestMapping("products.json")
    @ResponseBody
    public List getProduct() {
        return productService.queryList();
    }

    @PostMapping("upload.do")
    public void upload(MultipartFile[] upFile, HttpServletResponse response) throws IOException {
        for (MultipartFile file : upFile) {
            String filename = file.getOriginalFilename(); // 获取文件名

            // 随机命名，防止重名覆盖现象
            // 随机32位字符串
            String uuid = UUID.randomUUID().toString().replace("-","");

            filename = uuid + filename;
            System.out.println(filename.substring(32));

            file.transferTo(new File("d:/"+filename));
        }

        response.getWriter().write("upload success!");
    }
}
